package com.axt.manager.action;

import java.util.Map;

import org.apache.struts2.json.annotations.JSON;

import com.axt.soa.action.*;
import com.axt.manager.bean.*;
import com.axt.soa.msg.types.MsgParam;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction extends BaseAction{
	private LoginBean loginBean = new LoginBean();
	private NodeBean nodeBean = new NodeBean();
	
	private String userName;
	private String passwdMd5;
	private long timestamp;
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPasswdMd5() {
		return passwdMd5;
	}

	public void setPasswdMd5(String passwdMd5) {
		this.passwdMd5 = passwdMd5;
	}

	@JSON(name="timestamp")
	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}
	
	@JSON(name="errcode")
	public int getErrcode() {
		return errcode;
	}
	
	@JSON(name="errinfo")
	public String getErrinfo() {
		return errinfo;
	}
	
	/**
	 * 获取时间戳
	 * @throws Exception
	 */
    public String queryTimestamp() throws Exception {
    	setTimestamp(loginBean.queryTimestamp());
    	setErrcode(loginBean.getErrcode());
		setErrinfo(loginBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
    
    /**
     * 登录验证
     * @throws Exception
     */
    public String login() throws Exception {
    	
    	// 登录失败
    	if (loginBean.login(userName, passwdMd5, timestamp) == -1) {
    		setErrcode(loginBean.getErrcode());
    		setErrinfo(loginBean.getErrinfo());
    		return MsgParam.FAILURE;
    	}
    		
    	// 登录成功
    	ActionContext actionContext = ActionContext.getContext();
    	Map<String, Object> session = actionContext.getSession();

    	session.put(MsgParam.PARA_USER_ID, loginBean.getUserId());
    	session.put(MsgParam.PARA_ROLE_ID, loginBean.getRoleId());
    	session.put(MsgParam.PARA_SES_KEY, loginBean.getSesKey());

    	setErrcode(loginBean.getErrcode());
		setErrinfo(loginBean.getErrinfo());
    	return MsgParam.SUCCESS;
    }
    
    /**
     * 退出登录
     */
    public String logout() throws Exception {
    	loginBean.logout();
    	setErrcode(loginBean.getErrcode());
		setErrinfo(loginBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
}
